<template>
  <div class="app-container">
    <!-- 放置表单 -->
    <el-form ref="nameForm" label-width="60px" :model="userInfo" :rules="rules">
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="userInfo.username" style="width: 300px" />
      </el-form-item>
      <el-form-item label="密码:">
        <el-input v-model="userInfo.password" style="width: 300px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveUserDetailById">更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { saveUserDetailById } from '@/api/employee.js'
export default {
  props: {
    userInfo: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      rules: {
        username: [{ required: true, min: 3, max: 10, message: '用输入请输入3到10位字符', trigger: ['change', 'blur'] }]
      }
    }
  },
  methods: {
    /**
     * 更新用户信息
     * 1.等待整体效验完成才更新 ===>拿到组件实例调用validate方法
     * 2.调接口
     * 3.更新完毕成功提示并返回上次页面
     * 4.因为有数据回显所有没必要清空效验
     */
    async saveUserDetailById () {
      try {
        await this.$refs.nameForm.validate()
        await saveUserDetailById(this.userInfo)
        this.$message.success('更新成功')
        // 更新完成返回列表页面
        this.$router.back()
      } catch (error) {
        this.$message.error('更新失败!')
      }
    }
  }
}
</script>
